package com.hongning.railway.ui.main.fragments.college.exam;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.app.Constant;
import com.hongning.railway.databinding.FragmentExamresultBinding;
import com.hongning.railway.entity.reqbean.ReqAddExamScore;
import com.hongning.railway.entity.resbean.ResAnswerSubmitBean;
import com.hongning.railway.util.Apputils;

import java.text.NumberFormat;

import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Author: zhangjing
 * DATA:2019-08-20 15:40
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class ExamResultFragment extends BaseFragment<FragmentExamresultBinding, BaseViewModel> {
    ResAnswerSubmitBean resAnswerSubmitBean;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_examresult;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initParam() {
        super.initParam();
        resAnswerSubmitBean = getArguments().getParcelable(Constant.EXTRA_DATA);
    }

    @Override
    public void initData() {
        super.initData();
        binding.includeTitle.ivLeft.setVisibility(View.VISIBLE);
        binding.includeTitle.ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        initUI();
    }

    /**
     * 显示界面
     */
    private void initUI() {
        if (resAnswerSubmitBean!=null){
            if (resAnswerSubmitBean.getExamType().equals("1")){
                binding.includeTitle.tvTopTitle.setText("专项答题");
            }else {
                binding.includeTitle.tvTopTitle.setText("每周答题");
            }
            binding.tvNum.setText(resAnswerSubmitBean.getResult()+"");
            binding.tvRight.setText("正确题数:"+resAnswerSubmitBean.getResult()+"/"+resAnswerSubmitBean.getTotalTopicNum()+"题");
            binding.tvTime.setText("考试用时："+resAnswerSubmitBean.getAnswerConsuming());
            String rate = percentString(Double.valueOf(resAnswerSubmitBean.getResult()),Double.valueOf(resAnswerSubmitBean.getTotalTopicNum()));
            binding.tvRate.setText(rate);
            binding.tvScore.setText("活动积分：+"+resAnswerSubmitBean.getScore()+"分");
            binding.btnBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().finish();
                }
            });

            binding.btnCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.EXTRA_DATA,resAnswerSubmitBean.getExamId()+"");
                    bundle.putInt(Constant.EXTRA_INT,Integer.parseInt(resAnswerSubmitBean.getExamType()));
                    bundle.putString(Constant.EXTRA_STRING,resAnswerSubmitBean.getId()+"");
                    startContainerActivity(CheckAnswerFragment.class.getCanonicalName(),bundle);
                }
            });
        }
        addExamData();
    }

    public String percentString(double num1, double num2){
        if(Double.compare(num2, 0) == 0)
            throw new RuntimeException("输入有误");
        double ratio = num1 / num2;
        NumberFormat format = NumberFormat.getPercentInstance();
        format.setMaximumFractionDigits(2);//设置保留几位小数
        String result = "正确率："+format.format(ratio);
        return result;
    }

    private void addExamData(){
        ReqAddExamScore reqAddExamScore = new ReqAddExamScore();
        if (resAnswerSubmitBean.getExamType().equals("1")){
            reqAddExamScore.setExamType("001001001006");
        }else {
            reqAddExamScore.setExamType("001001001005");
        }
        reqAddExamScore.setScore(resAnswerSubmitBean.getScore()+"");
        reqAddExamScore.setTotleScore(resAnswerSubmitBean.getTotalPoints()+"");
        reqAddExamScore.setStaffId(Apputils.getStaffid());
        AppViewModelFactory.getInstance(getActivity().getApplication())
                .getmRepository()
                .addExamScore(reqAddExamScore)
                .compose(RxUtils.schedulersTransformer())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Object o) throws Exception {

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }
}
